'use strict'

const crypto = require('crypto')
const moment = require('moment')

// 加密
const handleEnCrypt = (data, salt, type = 'md5') => {
  return crypto.createHash(type).update(data + salt).digest('hex')
}

const dateFormat = (date, type = 'yyyy-MM-DD') => {
  return moment(date).format(type)
}

//  成功统一响应
const success = (ctx, res = {}) => {
  ctx.body = {
    code: res.code || 200,
    data: res.data || null,
    msg: res.msg || '请求成功'
  }
  ctx.status = 200
}

const sleep = (time = 3000) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, time)
  })
}

module.exports = {
  handleEnCrypt,
  dateFormat,
  success,
  sleep
}